package pe.gob.osinergmin.service.impl;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import pe.gob.osinergmin.bean.SupervisionMovil;
import pe.gob.osinergmin.dao.SupervisionMovilDAO;
import pe.gob.osinergmin.dao.impl.SupervisionMovilDAOImpl;
import pe.gob.osinergmin.service.SupervisionMovilService;
import pe.gob.osinergmin.utils.PuntoControlUtils;

public class SupervisionMovilServiceImpl implements SupervisionMovilService {

    private static SupervisionMovilDAO supervisionMovilDAO;

    private static SupervisionMovilServiceImpl instance;

    public SupervisionMovilServiceImpl() {
        super();
        supervisionMovilDAO = SupervisionMovilDAOImpl.getInstance();
    }

    @Override
    public SupervisionMovil findSupervisionMovil(Context context, SupervisionMovil supervisionMovil) throws Exception {
        if(supervisionMovil != null){
            List<SupervisionMovil> supervisionMovilList = getSupervisionMovilList(context, supervisionMovil);
            if(supervisionMovilList != null){
                return supervisionMovilList.get(0);
            }
        }
        return null;
    }

    @Override
    public List<SupervisionMovil> getSupervisionMovilList(Context context, SupervisionMovil supervisionMovil) throws Exception {
        List<SupervisionMovil> supervisionMovilList = supervisionMovilDAO.getSupervisionMovilList(context);
        List<SupervisionMovil> result = null;
        if(supervisionMovil != null){
            for(SupervisionMovil _supervisionMovil : supervisionMovilList){
                if(PuntoControlUtils.compareObjects(_supervisionMovil, supervisionMovil)){
                    if(result == null) result = new ArrayList<>();
                    result.add(_supervisionMovil);
                }
            }
        }else{
            result = supervisionMovilList;
        }
        return result;
    }

    @Override
    public void saveSupervisionMovilToLocal(Context context, SupervisionMovil supervisionMovil) throws Exception {
        List<SupervisionMovil> supervisionMovilList = getSupervisionMovilList(context, null);
        int index = -1;
        if(supervisionMovilList != null) {
            for (int i = 0; i < supervisionMovilList.size(); i++) {
                SupervisionMovil _supervisionMovil = supervisionMovilList.get(i);
                if (_supervisionMovil.getPlaca().equals(supervisionMovil.getPlaca()) && _supervisionMovil.getFechaRegistro().getTime() == supervisionMovil.getFechaRegistro().getTime()) {
                    index = i;
                    break;
                }
            }
        }else{
            supervisionMovilList = new ArrayList<>();
        }
        if(index != -1){
            supervisionMovilList.set(index, supervisionMovil);
        }else{
            supervisionMovilList.add(supervisionMovil);
        }
        saveSupervisionMovilListToLocal(context, supervisionMovilList);
    }

    @Override
    public void saveSupervisionMovilListToLocal(Context context, List<SupervisionMovil> supervisionMovilList) throws Exception {
        supervisionMovilDAO.saveSupervisionMovilListToLocal(context, supervisionMovilList);
    }

    @Override
    public void deleteSupervisionMovilListFromLocal(Context context) throws Exception {
        supervisionMovilDAO.deleteSupervisionMovilListFromLocal(context);
    }

    public static SupervisionMovilServiceImpl getInstance(){
        if(instance == null){
            instance = new SupervisionMovilServiceImpl();
        }
        return instance;
    }

    public Integer getSupervisionScopTotal(){
        return supervisionMovilDAO.getSupervisionScopTotal();
    }

    public Integer getSupervisionSeguridadTotal(){
        return supervisionMovilDAO.getSupervisionSeguridadTotal();
    }

    public Integer getSupervisionScopPendiente(){
        return supervisionMovilDAO.getSupervisionScopPendiente();
    }

    public Integer getSupervisionSeguridadPendiente(){
        return supervisionMovilDAO.getSupervisionSeguridadPendiente();
    }
}
